﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using iHouse.Model.Data;
using iHouse.Infrastructure;
using System.Linq;
using System.Text;

namespace iHouse.Web.Infrastructure
{
    public static class ModelExtension
    {
        public static NoteFormView ToNoteFormView(this Note note, IEnumerable<Category> categories, int selectValue)
        {
            return new NoteFormView
            {
                CategoryID = note.CategoryID,
                Description = note.Description,
                Title = note.Title,
                Reference = note.Reference,
                Categories = new SelectList(categories, "ID", "Name", selectValue)
            };
        }

        public static JqGridList<JsonCategoryEntity> ToJsonCategoryEntity(this JqGridList<Category> category)
        {
            JqGridList<JsonCategoryEntity> list = new JqGridList<JsonCategoryEntity>();
            list.PageIndex = category.PageIndex;
            list.PageSize = category.PageSize;
            list.TotalCount = category.TotalCount;
            list.TotalPages = category.TotalPages;

            List<JsonCategoryEntity> jsonCategories = new List<JsonCategoryEntity>();
            foreach (Category c in category.Rows)
            {
                jsonCategories.Add(new JsonCategoryEntity
                {
                    ID = c.ID,
                    Name = c.Name,
                    TypeID = c.TypeID,
                    Type = Enum.GetName(typeof(CategoryType), c.TypeID)
                });
            }

            list.Rows = jsonCategories.ToArray();

            return list;
        }

        public static AlbumFormView ToAlbumFormView(this Album obj, IEnumerable<Category> categories)
        {
            var category = categories.SingleOrDefault(x => x.ID == obj.CategoryID);

            return new AlbumFormView
            {
                CategoryID = obj.CategoryID,
                Category = category == null ? string.Empty : category.Name,
                ID = obj.ID,
                ImagePath = obj.ImagePath,
                LanguageID = obj.Language.Value,
                Language = Enum.GetName(typeof(LanguageType), obj.Language.Value),
                Location = obj.Location,
                Name = obj.Name,
                PublishDate = obj.PublishDate.HasValue ? obj.PublishDate.Value.ToShortDateString() : string.Empty,
                Singer = obj.Singer
            };
        }

        public static string ToJqGridOptions(this IEnumerable<Category> list)
        {
            StringBuilder sb = new StringBuilder();

            if (list.Count() == 0)
                return string.Empty;

            string format = "{0}:{1};";

            foreach (Category item in list)
            {
                sb.Append(string.Format(format, item.ID, item.Name));
            }

            return sb.ToString().TrimEnd(';');
        }

        public static string ToJqGridOptions(this Enum enumObj)
        {
            Type enumType = enumObj.GetType();

            StringBuilder sb = new StringBuilder();

            string format = "{0}:{1};";

            foreach (object item in Enum.GetValues(enumType))
            {
                sb.Append(string.Format(format, (int)item, Enum.GetName(enumType, item)));
            }

            return sb.ToString().TrimEnd(';');
        }
    }
}
